#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N=5e4+10;

struct node
{
	int l;
	int r;
	int index;
	int k;
}cows[N];
struct cmp
{
	bool operator()(pair<int,int>& a,pair<int,int>& b)
	{
		return a.first>b.first;
	} 
};
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>cows[i].l>>cows[i].r;
		cows[i].index=i;
	}
	sort(cows+1,cows+1+n,[](const node&a,const node&b)
		{
			return a.l<b.l;
		});
	priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> q;

	for(int i=1;i<=n;i++)
	{
		int right=cows[i].r;
		if(q.empty()||q.top().first>=cows[i].l)
		{
			int x=q.size()+1;
			q.push({right,x});
			cows[i].k=x;
		}
		else
		{
			int k=q.top().second;
			q.pop();
			q.push({right,k});
			cows[i].k=k;
		}
	}
	sort(cows+1,cows+1+n,[](const node&a,const node&b)
		{
			return a.index<b.index;
		});
	cout<<q.size()<<endl;
	for(int i=1;i<=n;i++)
	{
		cout<<cows[i].k<<endl;
	}
}